import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../common/models/ly_user_works_model.dart';
import '../../config/url_api.dart';
import '../../http/http_utils.dart';
import '../../modules/login/models/ly_login_info_manager.dart';
import '../../modules/login/models/ly_login_model.dart';
import '../../utils/ly_data_manager.dart';

class LYPersonalCenterController extends GetxController {
  UserInfo? userInfo = LYLoginInfoManager.getInstance.loginModel?.userInfo;
  List<LYWorksModel>? worksList;

  @override
  void onInit() {
    super.onInit();
    refreshUserInfo();
    refreshWorks();
  }

  void refreshWorks() {
    LYDataManager.share.loadMyWorks().then((value) {
      worksList = value;
      update(['works']);
    });
  }

  void refreshUserInfo() async {
    EasyLoading.show(maskType: EasyLoadingMaskType.black, dismissOnTap: true);
    final res = await HttpUtils.getInstance.get(URLApi.getUserInfo,
        queryParameters: {'userId': userInfo?.userId ?? ''});
    if (res.isSuccess) {
      final userModel = UserInfo.fromJson(res.data);
      LYLoginInfoManager.getInstance.updateUserInfo(userModel);
      userInfo = userModel;
      update(['userInfo']);
    }
    EasyLoading.dismiss();
  }
}
